﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.CategoryManager;
using Jungle.Code.ProductManager;
using Jungle.Code.BookManager;
using System.Net;

namespace Jungle.Code.BookManager.WebControls
{
    public class BookListUserPart: WebControl
    {
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            int catId = -1;
            string cmd = "";
            int bId = -1;

            string linkStr = Context.Request.RawUrl;
            linkStr = linkStr.Substring(0, linkStr.IndexOf("books.aspx") + 10);


            if (Context.Request["catId"] != null)
            { catId = Convert.ToInt32(Context.Request["catId"]); }
            if (Context.Request["cmd"] != null)
            { cmd = Context.Request["cmd"]; }
            if (Context.Request["bId"] != null)
            { bId = Convert.ToInt32(Context.Request["bId"]); }

            Book book = new Book();

            #region BooksListTable


            if (cmd != "view")
            {
                book.GetUnHidden();

                foreach (Book b in book.BooksList)
                {
                    writer.AddAttribute("class", "catalog_item");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute("style", "border:1px; cellpadding:0px; cellspacing:0px;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);
                    //TABLE


                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute("class", "books_table_td_image");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //картинка
                    if (b.ImagePath != "no_photo")
                    {
                        writer.AddAttribute("src", b.ImagePath);
                    }
                    else { writer.AddAttribute("src", "/UserFiles/Images/Content/no_photo.jpg"); }
                    writer.RenderBeginTag(HtmlTextWriterTag.Img); // img
                    writer.RenderEndTag();// img
                    writer.RenderEndTag(); // картинка

                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //детали

                    writer.RenderBeginTag(HtmlTextWriterTag.Table); // tsble ifo
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //название
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); // td
                    writer.Write("<strong>Название: </strong>" + b.Title);
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); //название

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //автор
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.Write("<strong>Автор: </strong>" + b.Author);
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); //автор

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //издательство
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.Write("<strong>Издатель: </strong>" + b.Publisher);
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); // издательство

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //год
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.Write("<strong>Год: </strong>" + b.PubYear);
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); // год

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //язык
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.Write("<strong>Язык: </strong>" + b.Language);
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); // язык

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); // more
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/books.aspx?cmd=details&bId=" + b.Id.ToString());
                    writer.AddAttribute("class", "books_more_link");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write("<strong>Подробнее... </strong>");
                    writer.RenderEndTag();
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); // more

                    writer.RenderEndTag(); //table info

                    writer.RenderEndTag(); //детали

                    writer.RenderEndTag();                   


                    //TABLE
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "clear2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();

                    //separator
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:10px; margin-top:0px;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:100%;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_left.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "separator_middle");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("&nbsp;");
                    writer.RenderEndTag();


                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_right.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    //separator end
                }

            }
            

            #endregion

            base.RenderContents(writer);
        }
        //--------------------------------------------------------------------
    }
}
